package top.yxqz.springmvc.msg.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: 余小小
 * @date: 2023/4/26
 * @description：域对象共享数据
 */
@Controller
@RequestMapping("/msg")
public class MsgController {

    /**
     * 1.使用request请求域对象向Request域对象中添加数据
     * uri: /msg/servlet
     */
    @GetMapping("/servlet")
    public String methodName(HttpServletRequest request) {
        request.setAttribute("username", "余小小");
        return "success";
    }

    /**
     * 2.使用ModelANDView向Request域对象中添加数据
     * uri: /msg/mav
     */
    @GetMapping("/mav")
    public ModelAndView msgeBYModelAndView(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
//        a.数据
        mv.addObject("usernamemv", "余小小(ModelAndView)");
//        b.视图
        mv.setViewName("success");
        return mv;
    }

    /**
     * 3.使用Model向Request域对象中添加数据
     * uri: /msg/mav
     */
    @GetMapping("/mav/model")
    public String methodName(Model model) {
        model.addAttribute("username_model", "余小小(Model)");
        return "success";
    }

    /**
     * 4.使用ModelMap向Request域对象中添加数据
     * uri: /msg/mav/modelMap
     */
    @GetMapping("/mav/modelMap")
    public String modelMap(ModelMap modelMap) {
        modelMap.addAttribute("username_modelMap", "余小小(ModelMap)");
        return "success";
    }
    /**
     * 5.使用map向Request域对象中添加数据
     */
    @GetMapping("/mav/map")
    public String  map(Map<String,String> map) {
            map.put("username_map", "余小小(Map)");
            return "success";
    }
}
